<?php

class Admin_SignController extends My_Controller_ActionAdmin {

    public function isAllowedAction($controllerName, $actionName) {
        return true;
    }

    public function inAction() {
        $form = new Admin_Form_Login();

        if (!$this->_request->isPost() || !$form->isValid($_POST)) {
            $this->view->form = $form;
            return;
        }

        $data = $form->getValues();
        $email = $data['email'];
        $pass = $data['pass'];
        $daoUser = new Application_Model_DAO_User();
        $user = $daoUser->login($email, $pass);
        if (empty($user)) {
            $this->addError($this->_('XXX_USER_LOGIN_INVALID_LOGIN_DATA'));
            $this->view->form = $form;
            $this->setPrintCurrentFlashMessages();
            return;
        }
        $this->addSuccess($this->_('XXX_USER_LOGIN_SUCCESS'));

        $url = $this->getURLWhileLogin();
        if (!$url) {
            $url = $this->url(array('controller' => 'index', 'action' => 'index'), 'admin');
        }
        $this->_redirect($url);
    }

    public function outAction() {
        $this->setCurrentUser();
        Zend_Auth::getInstance()->clearIdentity();
        // todo delete cookies
        $this->addSuccess($this->_('XXX_USER_LOGOUT_SUCCESS'));
        $url = $this->url(array('controller' => 'index', 'action' => 'index'), 'admin');
        $this->_redirect($url);
    }
}

